//
//  ListFoodTableView.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 8/1/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "ListFoodTableView.h"
#import "ZooUtil.h"

@implementation ListFoodTableView
#define AVATAR_FOOD_SIZE 70
#define IMG_LIKE_SIZE 24
#define BUTTON_HEIGHT 45
#define BUTTON_WIDTH 74

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame style:UITableViewStylePlain];
    if (self) {
        
        _arFood = [[NSMutableArray alloc]initWithObjects:@"Bun tron thit bo", @"Pho cuon1",@"Pho cuon2",@"Pho cuon3",@"Pho cuon4",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon",@"Pho cuon end 3",@"Pho cuon end 2",@"Pho cuon end 1",@"Pho cuon end", nil];
        self.delegate = self;
        self.dataSource = self;

    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
        case 2:
            return 1;
            break;
        case 1:
            return [_arFood count];
            break;
        default:
            break;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier0 = @"cellIdentifier0";
    NSString *cellIdentifier1 = @"cellIdentifier1";
    NSString *cellIdentifier2 = @"cellIdentifier2";
    
    UITableViewCell *_cell = nil;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    switch (indexPath.section) {
        case 0:
            _cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0];
            if (_cell == nil) {
                _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier0];
                [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];

            }

            [self initHeaderTable:_cell];
            break;
        case 1:
            _cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
            if (_cell == nil) {
                _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];    
                [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];

            }
            
            [self initIntroductionWithCell:_cell indexPath:indexPath.row];
            break;
        case 2:
            _cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
            if (_cell == nil) {
                _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2];
                [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];

            }
            
            [self initFooterTable:_cell];
            break;
        default:
            break;
    }
    
    return _cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return 50;
        case 1:
            return 140;
        case 2:
            return 100;
        default:
            break;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (void)initHeaderTable:(UITableViewCell *)pCell {
    _vHeader = [[UIView alloc]initWithFrame:CGRectMake(11, 8, 298, 39)];
    [_vHeader setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_SEARCH]]];
    
    _btnSearch = [[UIButton alloc]initWithFrame:CGRectMake(8, 8, 24, 24)];
    [_btnSearch setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_BTN_SEARCH]]];
    
    _tfSearch = [[UITextField alloc]initWithFrame:CGRectMake(45, 8, 240, 20)];
    [_tfSearch setPlaceholder:STRING_SEARCH_HINT];
    _tfSearch.textAlignment = UITextAlignmentLeft;
    
    [_vHeader addSubview:_btnSearch];
    [_vHeader addSubview:_tfSearch];
    
    [pCell.contentView addSubview:_vHeader];
}

- (void)initIntroductionWithCell:(UITableViewCell *)pCell indexPath:(NSInteger)indexPath{
    _vInfoFood = [[UIImageView alloc]init];
    _vImgFood =  [[UIImageView alloc]init];
    _vImgLike =  [[UIImageView alloc]init];
    _vInfoFood.frame = CGRectMake(10, 5, 299, 81);

    [_vInfoFood setImage:[UIImage imageNamed:IMG_LISTFOOD_BACKGROUND]];
    
    _vImgFood.frame = CGRectMake(5, 5, AVATAR_FOOD_SIZE, AVATAR_FOOD_SIZE);
    [_vImgFood setImage:[UIImage imageNamed:@"food1.png"]];
    
    _lbNameFood = [[UILabel alloc]initWithFrame:CGRectMake(AVATAR_FOOD_SIZE+10, 5, 210, 20)];
    [_lbNameFood setFont:[UIFont boldSystemFontOfSize:15]];
    [_lbNameFood setBackgroundColor:[UIColor clearColor]];
    [_lbNameFood setText:[_arFood objectAtIndex:indexPath]];
    
    _vImgLike.frame = CGRectMake(AVATAR_FOOD_SIZE+10, 35, IMG_LIKE_SIZE, IMG_LIKE_SIZE);
    [_vImgLike setImage:[UIImage imageNamed:IMG_LIKE]];
    
    _lbPersonLike = [[UILabel alloc]initWithFrame:CGRectMake(AVATAR_FOOD_SIZE+IMG_LIKE_SIZE+12, 32, 100, 30)];
    [_lbPersonLike setFont:[UIFont systemFontOfSize:16]];
    [_lbPersonLike setBackgroundColor:[UIColor clearColor]];
    [_lbPersonLike setText:@"+15"];
    [_lbPersonLike setTextAlignment:UITextAlignmentLeft];
    [_lbPersonLike setTextColor:[ZooUtil getColorFromHexa:@"2e8193" alpha:1.0]];
    
    _lbCost = [[UILabel alloc]initWithFrame:CGRectMake(215, 30, 75, 30)];
    [_lbCost setFont:[UIFont systemFontOfSize:21]];
    [_lbCost setText:@"35K"];
    [_lbCost setTextColor:[ZooUtil getColorFromHexa:@"ba3129" alpha:1.0]];

    [_vInfoFood addSubview:_lbNameFood];
    [_vInfoFood addSubview:_lbPersonLike];
    [_vInfoFood addSubview:_vImgFood];
    [_vInfoFood addSubview:_vImgLike];
    [_vInfoFood addSubview:_lbCost];
    
    _btnEat = [[UIButton alloc]initWithFrame:CGRectMake(10, 88, BUTTON_WIDTH, BUTTON_HEIGHT)];
    [self setupButton:_btnEat background:IMG_LISTFOOD_BTN_BACKGROUND viewIcon:_vImgEat nameIcon:IMG_BTN_LISTFOOD_EAT label:_lbEat labelContent:@"Đã ăn"];
    
    _btnWishList = [[UIButton alloc]initWithFrame:CGRectMake(10 + BUTTON_WIDTH+1, 88, BUTTON_WIDTH, BUTTON_HEIGHT)];
    [self setupButton:_btnWishList background:IMG_LISTFOOD_BTN_BACKGROUND viewIcon:_vImgWishList nameIcon:IMG_BTN_LISTFOOD_WISHLIST label:_lbWishList labelContent:@"Thêm"];
    
    _btnLike = [[UIButton alloc]initWithFrame:CGRectMake(10 + (BUTTON_WIDTH+1)*2, 88, BUTTON_WIDTH, BUTTON_HEIGHT)];
    [self setupButton:_btnLike background:IMG_LISTFOOD_BTN_BACKGROUND viewIcon:_vImgLike nameIcon:IMG_BTN_LISTFOOD_LIKE label:_lbLike labelContent:@"Thích"];

    
    _btnDetailFood = [[UIButton alloc]initWithFrame:CGRectMake(10 + (BUTTON_WIDTH+1)*3, 88, BUTTON_WIDTH, BUTTON_HEIGHT)];
    [self setupButton:_btnDetailFood background:IMG_LISTFOOD_BTN_BACKGROUND viewIcon:_vImgDetailFood nameIcon:IMG_BTN_LISTFOOD_DETAIL label:_lbDetailFood labelContent:@"Chi tiết"];

//    [_btnWishList setBackgroundImage:[UIImage imageNamed:IMG_LISTFOOD_BTN_BACKGROUND] forState:UIControlStateNormal];
//    _vImgWishList = [[UIImageView alloc]initWithFrame:CGRectMake(25, 5, 24, 24)];
//    [_vImgWishList setImage:[UIImage imageNamed:IMG_BTN_LISTFOOD_WISHLIST]];
//    _lbWishList = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, BUTTON_WIDTH, 15)];
//    
//    [_lbWishList setBackgroundColor:[UIColor clearColor]];
//    [_lbWishList setFont:[UIFont systemFontOfSize:15]];
//    [_lbWishList setText:@"Thêm"];
//    [_lbWishList setTextAlignment:UITextAlignmentCenter];
//    [_lbWishList setTextColor:[UIColor whiteColor]];
//    [_btnWishList addSubview:_vImgWishList];
//    [_btnWishList addSubview:_lbWishList];  
    
    [pCell.contentView addSubview:_vInfoFood];
    [pCell.contentView addSubview:_btnEat];
    [pCell.contentView addSubview:_btnLike];
    [pCell.contentView addSubview:_btnWishList];
    [pCell.contentView addSubview:_btnDetailFood];
    
    [_vInfoFood release];
    [_vImgFood release];
    [_vImgLike release];
    [_lbPersonLike release];
    [_lbNameFood release];
    [_lbCost release];
    
//    [_btnWishList release];
//    [_btnLike release];
//    [_btnEat release];
//    [_btnDetailFood release];  
//    [_vImgLike release];
//    [_vImgDetailFood release];
//    [_vImgWishList release]; 
//    [_vImgEat release];    
//    [_lbDetailFood release];
//    [_lbWishList release];    
//    [_lbEat release];
//    [_lbLike release];    
    
}

- (void)setupButton:(UIButton *)pButton background:(NSString *)pNameBackground viewIcon:(UIImageView *)pViewIcon nameIcon:(NSString *)pNameIcon label:(UILabel *)pLabel labelContent:(NSString *)pContent {
    [pButton setBackgroundImage:[UIImage imageNamed:pNameBackground] forState:UIControlStateNormal];
    pViewIcon = [[UIImageView alloc]initWithFrame:CGRectMake(25, 5, 24, 24)];
    [pViewIcon setImage:[UIImage imageNamed:pNameIcon]];
    pLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, BUTTON_WIDTH, 15)];
    
    [pLabel setBackgroundColor:[UIColor clearColor]];
    [pLabel setFont:[UIFont systemFontOfSize:15]];
    [pLabel setText:pContent];
    [pLabel setTextAlignment:UITextAlignmentCenter];
    [pLabel setTextColor:[UIColor whiteColor]];
    [pButton addSubview:pViewIcon];
    [pButton addSubview:pLabel];
    
    [pViewIcon release];
    [pLabel release];
}

- (void)initFooterTable:(UITableViewCell *)pCell {
    NSLog(@"footer");
    _vHeader = [[UIView alloc]initWithFrame:CGRectMake(11, 8, 298, 39)];
    
    [pCell.contentView addSubview:_vHeader];
}

- (void)dealloc {
    [super dealloc];
}
@end
